package com.niiwoo.civet.account.service.local.refund;

import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.niiwoo.civet.account.dao.mapper.BatchTransOrderDetailMapperExt;
import com.niiwoo.civet.account.dao.mapper.BatchTransOrderMapperExt;
import com.niiwoo.civet.account.dto.common.AccountBatchTransOrderDTO;
import com.niiwoo.civet.trade.enums.BatchRefundTypeEnum;
import com.niiwoo.civet.trade.enums.RepayStatusEnum;
import com.niiwoo.tripod.lanmao.component.LanMaoDirectService;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by dell on 2018/1/10.
 */
@Service
@Slf4j
public class AccountRefundCommonLocalService {

    @Autowired
    private LanMaoDirectService lanMaoDirectService;

    @Autowired
    private BatchTransOrderMapperExt batchTransOrderMapperExt;

    @Autowired
    private BatchTransOrderDetailMapperExt batchTransOrderDetailMapperExt;


    /**
     * 强制主库状态查询
     * @param dto
     * @return
     */
    public int selectFromMasterRefundingProject(AccountBatchTransOrderDTO dto){

        if (!HintManagerHolder.isMasterRouteOnly()) {
            HintManagerHolder.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.setMasterRouteOnly();
        }
        int hasHandled = batchTransOrderMapperExt.queryOrderByProIdAndPeriod(dto.getProjectId(), dto.getCurrentPeriod());
        //大于0表示之前处理过,忽略,直接处理下一个还款
        if (hasHandled > 0) {
            log.error("查询标的ID和当前期数在还款订单表中,标的号{}", dto.getProjectId());
            //提前结清，逾期还款，坏账还款，都支持一天多次还款
            if (dto.getBatchRefundType().equals(BatchRefundTypeEnum.OVER_DEBT.getValue())
                    || dto.getRepayStatus().equals(RepayStatusEnum.BADLOANS.getCode())
                    || dto.getRepayStatus().equals(RepayStatusEnum.OVERDUE.getCode())
                    || dto.getBatchRefundType().equals(BatchRefundTypeEnum.GUARANTEE.getValue())
                    || dto.getBatchRefundType().equals(BatchRefundTypeEnum.NEED_GUARANTEE_REFUND.getValue())){
                log.info("用户已有还款记录，再次发起还款projectId={}",dto.getProjectId());

                if (!HintManagerHolder.isMasterRouteOnly()) {
                    HintManagerHolder.clear();
                    HintManager hintManager = HintManager.getInstance();
                    hintManager.setMasterRouteOnly();
                }
                int hasHandled2 = batchTransOrderMapperExt.queryOrderByProIdHanding(dto.getProjectId());
                if (hasHandled2 > 0){
                    log.error("标的号{}，有还款正在处理中",dto.getProjectId());
                    throw new BizException("ACC10025");
                }
            }else {
                log.error("标的号{}，按期还款正在处理中",dto.getProjectId());
                throw new BizException("ACC10025");
            }
        }else {
            log.info("还款订单已经执行过,batchRefundType={},标的号{},期数{}",dto.getBatchRefundType(),dto.getProjectId(),dto.getCurrentPeriod());
        }
        return hasHandled;
    }

}
